package com.hcrazy.manchester.app.controllers.member;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.Message;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.MessageService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 消息部分
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api")
public class MessageAPIController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 发送消息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/message",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object sendMessage(
			HttpServletRequest req,
			@RequestParam long toUserId,
			@RequestParam String message){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能发送消息！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			
			Message msg = new Message();
			msg.setFromUserId(userId);
			msg.setMessage(message);
			msg.setMessageType(Constant.MESSAGE_TYPE_USER);
			msg.setStatus(Constant.MESSAGE_STATUS_NORMAL);
			msg.setToUserId(toUserId);
			
			return messageService.addMessage(msg);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/**
	 * 删除消息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/message/del/{messageId}",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object delMessage(
			@PathVariable long messageId,
			HttpServletRequest req){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能发送消息！");
			}
			
			messageService.deleteMessage(messageId, 0);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/message/recover/{messageId}",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object recoverMessage(
			@PathVariable long messageId,
			HttpServletRequest req){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能发送消息！");
			}
			
			messageService.recoverMessage(messageId);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/messages",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllMessages(
			HttpServletRequest req){
	
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能获取消息！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			
			return messageService.getMessages(Constant.MESSAGE_STATUS_NORMAL, userId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/toMineMessages",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object toMineMessages(
			@RequestParam(required=false) Integer messageType,
			@RequestParam(required=false) Integer status,
			HttpServletRequest req){
	
		if(null == messageType)
			messageType = -1;
		if(null == status)
			status = 0;
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能获取消息！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			PageModel model = new PageModel();
			return messageService.getPagedMessages(model, messageType,userId,status);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/friendMessages",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object friendMessages(
			@RequestParam(required=false) Integer messageType,
			@RequestParam(required=false) Integer status,
			@RequestParam long friendId,
			HttpServletRequest req){
	
		if(null == messageType)
			messageType = Constant.MESSAGE_TYPE_USER;
		if(null == status)
			status = Constant.MESSAGE_STATUS_NORMAL;
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能获取消息！");
			}
			User user = (User)obj;
			long userId = user.getUserId();

			return messageService.getFriendMessages(messageType, userId, friendId, status);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/fromMineMessages",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object fromMineMessages(
			@RequestParam(required=false) Integer status,
			HttpServletRequest req){

		if(null == status)
			status = 0;
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不能获取消息！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			PageModel model = new PageModel();
			return messageService.getPagedMessages2(model, Constant.MESSAGE_TYPE_USER,userId,status);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	
	
	/**
	 * 系统参数异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public @ResponseBody ErrorMsg getException(Exception e){
		Log.error(e.getMessage(), e);
		return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
	}
	
	private Map<String,String> setResult(String value){
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,value);
		return result;
	}
}
